/*
package com.echo.aichat.ai.agent;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.function.Function;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.stream.Collectors;

@Agent
@Description("获取微博热搜信息并进行热门指数评估和趋势分析的智能体")
public class TrendingSearchEvaluator extends AbstractAgent<TrendingSearchEvaluator.Request, String> {
    private final String SYSTEM = """
            你是一个专业的搜索智能体，能够实时搜索微博热搜榜上的信息，
            并对这些信息的热门程度和趋势进行详细分析，提供深度见解。
            """;

    public TrendingSearchEvaluator(ChatModel chatModel) {
        super(chatModel);
    }

    @Override
    public String apply(Request request) {
        // 整合搜索和评估功能，返回最终综合结果
        return getChatClient()
                .prompt()
                .system(SYSTEM)
                .user(request.query)
                .call()
                .content();
    }

    public record Request(
            @JsonProperty(required = true)
            @JsonPropertyDescription("用户原始的查询请求") String query) {
    }

    @Component
    @Description("搜索当前微博热搜信息")
    public static class SearchHotTrends implements Function<SearchHotTrends.Request, String> {
        @Override
        public String apply(Request request) {
            HttpClient client = HttpClient.newHttpClient();
            String apiUrl = "https://weibo.com/ajax/side/hotSearch";
            try {
                HttpRequest httpRequest = HttpRequest.newBuilder()
                        .uri(new URI(apiUrl))
                        .GET()
                        .build();
                HttpResponse<String> response = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
                String responseBody = response.body();

                // 使用 Jackson 解析响应数据
                ObjectMapper mapper = new ObjectMapper();
                WeiboHotSearchResponse hotSearchResponse = mapper.readValue(responseBody, WeiboHotSearchResponse.class);

                if (hotSearchResponse.getOk() != 1 || hotSearchResponse.getData() == null || hotSearchResponse.getData().getRealtime() == null || hotSearchResponse.getData().getRealtime().isEmpty()) {
                    return "{\"success\": false, \"msg\": \"未能获取有效的微博热搜数据\", \"data\": []}";
                }

                // 提取需要的数据
                StringBuilder hotSearchData = new StringBuilder();
                for (HotTrend trend : hotSearchResponse.getData().getRealtime()) {
                    hotSearchData.append(String.format("标题: %s\n热度指数: %d\n链接: %s\n\n",
                            trend.getWord(), trend.getNum(), "https://s.weibo.com/weibo?q=" + trend.getWord()));
                }

                return hotSearchData.toString();
            } catch (Exception e) {
                return "{\"success\": false, \"msg\": \"" + e.getMessage() + "\", \"data\": []}";
            }
        }

        public record Request(
                @JsonProperty("dummy") String dummy
        ) {}

        // 微博热搜数据响应结构
        public static class WeiboHotSearchResponse {
            private int ok;
            private Data data;

            @JsonProperty("ok")
            public int getOk() {
                return ok;
            }

            public void setOk(int ok) {
                this.ok = ok;
            }

            @JsonProperty("data")
            public Data getData() {
                return data;
            }

            public void setData(Data data) {
                this.data = data;
            }

            public static class Data {
                private List<HotTrend> realtime;

                @JsonProperty("realtime")
                public List<HotTrend> getRealtime() {
                    return realtime;
                }

                public void setRealtime(List<HotTrend> realtime) {
                    this.realtime = realtime;
                }
            }
        }

        // 单个热搜项的结构
        public static class HotTrend {
            private String word;
            private int num;

            @JsonProperty("word")
            public String getWord() {
                return word;
            }

            public void setWord(String word) {
                this.word = word;
            }

            @JsonProperty("num")
            public int getNum() {
                return num;
            }

            public void setNum(int num) {
                this.num = num;
            }
        }
    }

    @Component
    @Description("评估微博热搜信息的热门指数")
    public static class EvaluateTrends implements Function<EvaluateTrends.Request, String> {
        @Override
        public String apply(Request request) {
            // 调用SearchHotTrends获取实时热搜信息
            SearchHotTrends searchHotTrends = new SearchHotTrends();
            String trendingData = searchHotTrends.apply(new SearchHotTrends.Request("dummy"));

            return trendingData;  // 直接返回热搜数据字符串
        }

        public record Request(
                @JsonProperty("dummy") String dummy
        ) {}
    }
}
*/
